<?php
	//导入 配置信息
header('Content-Type:application/json; charset=utf8');
include("config.php");
//导入 登陆 信息 结构 
//include("./loginTypeClass.php");
class Login_type{
	public $status='error';//状态信息 success 是 处理 成功 error 是 处理失败
	public $type='login';//数据类型
	public $apikey='';
	public $userid=0;
}

session_start();
if(@$_SESSION['apikey']==''){
	$_SESSION['apikey']='guest';
}

if(@$_SESSION['userid']==''){
	$_SESSION['userid']=0;
}

$reqData=new Login_type();
if(@$_POST['username']&&@$_POST['password'])
{
	$dbconf=new DBConfig();
	$username=$_POST['username'];
	$password=$_POST['password'];
	$conn= new mysqli($dbconf->servername,$dbconf->username,$dbconf->password,$dbconf->dbname);
	if ($conn->connect_error) {
		   // die("mysql连接失败: " . $conn->connect_error);
		   $reqData->status='error';
		   $reqData->type='mysql';
	}
	
	$sql="SELECT * FROM `user` WHERE username ='$username' and password=md5('$password');";
//	echo $sql;
	$result = $conn->query($sql);
	if($result->num_rows>0){
		//查询 数据 库 返回的是 查询到的结果有 几条 
		//如果 大于 0 条  意味着存在这条数据 表面 数据有效 则 输出信息		
		$row =$result->fetch_assoc();
		$reqData->status='success';
		$reqData->apikey=$row['apikey'];
		$reqData->userid=$row['userid'];
		$_SESSION['apikey']=$row['apikey'];
		$_SESSION['userid']=$row['userid'];
		$_SESSION['username']=$row['username'];
	}else{
		$reqData->status='error';
		$reqData->apikey='';
		$reqData->userid='';
	}
}
echo json_encode($reqData);
?>
